//
//  GYVisitorDetailsViewController.m
//  Visitor
//
//  Created by x on 2017/9/13.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYVisitorDetailsViewController.h"
#import "GYDetailsTableViewCell.h"
#import "GYDetailsHeadView.h"
#import "GYDetailsFootView.h"
#import "GYRespondentsTableViewController.h"
#import "GYNetWorkTools.h"


@interface GYVisitorDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,GYDetailsFootViewDelegate>

/** 访客详情 */
@property (nonatomic, weak) UITableView *detailsTableView;

/** 标题数组 */
@property (nonatomic, strong) NSArray *titles;

/** 内容数组 */
@property (nonatomic, strong) NSMutableArray *contens;

/** 表格头视图 */
@property (nonatomic, weak) GYDetailsHeadView *headView;

/** 表格尾视图 */
@property (nonatomic, weak) GYDetailsFootView *footView;

/** 保存处理状态 */
@property (nonatomic, strong) NSString *handleState;
@end

@implementation GYVisitorDetailsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGBColor(0XF0F0F0);
    self.navigationItem.title = @"访客详情";
    
    //初始化表格
    [self setupTableView];
    
}
- (void)setupTableView {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 49;
    tableView.backgroundColor = [UIColor clearColor];
    //设置表格的头视图和尾视图
    GYDetailsHeadView *headView = [[GYDetailsHeadView alloc] init];
    headView.frame = CGRectMake(0, 0, WIDTH,[headView headViewHeight]);
    headView.model = _model;
    tableView.tableHeaderView = headView;
    self.headView = headView;
    
    GYDetailsFootView *footView = [[GYDetailsFootView alloc] init];
    footView.model = self.model;
    footView.delegate = self;
    footView.frame = CGRectMake(0, 0, WIDTH, [footView footViewHeight]);
    tableView.tableFooterView = footView;
    self.footView = footView;
    
    [self.view addSubview:tableView];
    
    self.detailsTableView = tableView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //3.监听键盘frame的改变
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)keyboardWillChangeFrame:(NSNotification *)noti {
    
    CGRect keyBoardRect = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationTime = [[noti.object objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];
    //代表键盘弹出
    [UIView animateWithDuration:animationTime animations:^{
        if (keyBoardRect.origin.y < HEIGHT) {
            self.detailsTableView.frame = CGRectMake(0, -keyBoardRect.size.height, WIDTH, HEIGHT-64);
        }else {
            self.detailsTableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-64);
        }
    }];
}

#pragma mark -- <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GYDetailsTableViewCell *cell = [GYDetailsTableViewCell cellWithTableView:tableView];
    cell.title = self.titles[indexPath.row];
    cell.content = self.contens[indexPath.row];
    //1.判断该访客记录是否已经处理
    if (self.model.result == 0) {
        cell.show = YES;
    }else {
        if (indexPath.row > 1 ) {
            cell.show = NO;
        }else {
            cell.show = YES;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //1.判断该访客记录是否已经处理
    if (self.model.result == 0) {
        //代表未处理
//        if (indexPath.row == 2) {
//            GYRespondentsTableViewController *respondentsControlelr = [[GYRespondentsTableViewController alloc] init];
//            [self.navigationController pushViewController:respondentsControlelr animated:YES];
//        }else
        if (indexPath.row == 2) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *handle = [UIAlertAction actionWithTitle:@"同意" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.handleState = @"1";
                [self.contens replaceObjectAtIndex:indexPath.row withObject:@"同意"];
                [self.detailsTableView reloadData];
            }];
            UIAlertAction *refuse = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.handleState = @"2";
                [self.contens replaceObjectAtIndex:indexPath.row withObject:@"拒绝"];
                [self.detailsTableView reloadData];
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alertController addAction:handle];
            [alertController addAction:refuse];
            [alertController addAction:cancel];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }else {
        //代表已经处理
        
    }
}
#pragma  mark -- <GYDetailsFootViewDelegate>
- (void)uploadVisitorHandleState:(NSString *)cause {
    [GYNetWorkTools handleVisitorRequest:[[GYUserInfo loadUserInfo] requestKey] operationCode:self.handleState visitorId:self.model.visitorId cause:cause susscess:^(NSDictionary *obj) {
        if ([[obj objectForKey:@"status"] intValue] == 200) {
            //代表上传成功啊
            //1.更新model的状态
            self.model.result = [self.handleState intValue];
            self.headView.model = self.model;
            self.footView.model = self.model;
            self.visitorBlock(self.index, [self.handleState intValue]);
        }else {
            //重新刷新访客记录
            [[NSNotificationCenter defaultCenter]postNotificationName:@"updateVisitorInfo" object:nil];
        }
    } filed:^(NSError *error) {
        NSLog(@"handleVisitorRequest=======%@",error);
        [SVProgressHUD showImage:nil status:@"出现一个错误信息,请检查网络重新上传!"];
    }];
}

#pragma mark -- 懒加载

- (NSArray *)titles
{
    if (!_titles) {
        _titles = @[@"访客事由",@"手机号",@"状态"];
    }
    return _titles;
}

- (NSMutableArray *)contens
{
    if (!_contens) {
        _contens = [NSMutableArray arrayWithObjects:_model.content,_model.phone,@"可访问",@"处理", nil];
    }
    return _contens;
}

@end
